{ "cells": [ { "cell_type": "markdown", "id": "d26e3b44", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Decomposing Qiskit circuits" ] }, { "cell_type": "markdown", "id": "f5994d25", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "In this notebook, we show how Qiskit circuits can be converted into Perceval circuits. To do so, we take the example of a simple gate-based circuit (a circuit producing GHZ states) and we show the translation to a linear optical circuit. We also show the equivalence between the two circuits." ] }, { "cell_type": "markdown", "id": "3ed1c79e", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "As usual we start by imported the useful libraries. Note that this notebook requires the installation of Qiskit (which can be easiliy done with `pip install qiskit`)." ] }, { "cell_type": "code", "execution_count": 1, "id": "9dea723b", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "import perceval as pcvl\n", "from perceval.components import catalog\n", "from perceval.converters import QiskitConverter\n", "\n", "from qiskit import QuantumCircuit\n", "from qiskit.quantum_info import Statevector" ] }, { "cell_type": "markdown", "id": "61e8fe05", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## GHZ State generation in Qiskit" ] }, { "cell_type": "markdown", "id": "57423a74", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "We first define the circuit generating GHZ states of 3 qubits with Qiskit. To do so, we first act with a Hadamard gate on qubit 0 to put in superposition of state $|0\\rangle$ and $|1\\rangle$. Then we perform two CNOT gates using qubit 0 as control and qubits 1 and 2 as targets." ] }, { "cell_type": "code", "execution_count": 2, "id": "aef291d3", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": " ┌───┐ \nq_0: ┤ H ├──■────■──\n └───┘┌─┴─┐ │ \nq_1: ─────┤ X ├──┼──\n └───┘┌─┴─┐\nq_2: ──────────┤ X ├\n └───┘", "text/html": "
┌───┐ \nq_0: ┤ H ├──■────■──\n └───┘┌─┴─┐ │ \nq_1: ─────┤ X ├──┼──\n └───┘┌─┴─┐\nq_2: ──────────┤ X ├\n └───┘" }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a Quantum Circuit acting on the q register\n", "circuit = QuantumCircuit(3)\n", "\n", "# Add a H gate on qubit 0\n", "circuit.h(0)\n", "\n", "# Add CX (CNOT) gates on control qubit 0 and target qubits 1 and 2\n", "circuit.cx(0, 1)\n", "circuit.cx(0, 2)\n", "\n", "# Draw the circuit\n", "circuit.draw()" ] }, { "cell_type": "markdown", "id": "cca607ca", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "We display the final state when starting from the input state $|000\\rangle$." ] }, { "cell_type": "code", "execution_count": 3, "id": "7edaa2b3", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": "
state | probability |
---|---|
|1,0,1,0,1,0> | 1/2 |
|0,1,0,1,0,1> | 1/2 |
|1,0,0,1,1,0> | 0 |
|0,1,1,0,0,1> | 0 |